import * as actions from '../actions/index';

// 页面初始化数据
export const initialState = {
  settings: {},
  isLoadingSettings: true,
  pageSize: 10,
  tableListData: {},
  isLoadingList: true,
  syncStatus: false,
  isLoadingSyncStatus: true,
  lastMirrorSyncRecord: {},
};
const defaultAction = {
  type: 'doNothing',
};

export default function index(state = initialState, action = defaultAction) {
  switch (action.type) {
    case actions.UPDATE_DATA:
      return { ...state, ...action.data };
    case actions.RECEIVE_MiRROR_SETTINGS:
      return { ...state, settings: action.data, isLoadingSettings: false };
    case actions.RECEIVE_MiRROR_LIST:
      return { ...state, tableListData: action.data, isLoadingList: false };
    case actions.RECEIVE_MiRROR_STATUS:
      return { ...state, syncStatus: action.data, isLoadingSyncStatus: false };
    case actions.RECEIVE_LAST_MiRROR_RECORD:
      return { ...state, lastMirrorSyncRecord: action.data };
    case actions.TEST_RESULT:
      return { ...state, webhooks: action.data };
    case actions.REMOVE_WEBHOOK:
      return { ...state, webhooks: state.webhooks.filter((e) => e.id !== action.data) };
    case actions.CREATE_WEBHOOK: {
      const orgWebhooks = state.webhooks.filter((v) => v.type === 'OrganizationHook');
      const proWebhooks = state.webhooks.filter((v) => v.type !== 'OrganizationHook');
      proWebhooks.unshift(action.data);
      return { ...state, webhooks: orgWebhooks.concat(proWebhooks) };
    }
    default:
      return state;
  }
}
